//
//  JPWelcomViewController.swift
//  JPWeibo
//
//  Created by 哇咔咔 on 16/9/23.
//  Copyright © 2016年 jp. All rights reserved.
//

import UIKit
import SDWebImage

let SwitchRootVCNotification = "SwitchRootVCNotification"

class JPWelcomViewController: UIViewController {
    
    lazy var iconImageView: UIImageView = {
        
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = icon.width / 2
        icon.layer.masksToBounds = true
        
        if let iconUrlStr = JPUserInfoViewModel.sharedUserInfoViewModel.userInfo?.avatar_large {
            
            icon.sd_setImage(with: URL(string: iconUrlStr), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        return icon
    }()
    
    lazy var nameLabel: UILabel = {
       
        let name = UILabel()
        name.font = UIFont.systemFont(ofSize: 13)
        name.textColor = UIColor.darkGray
        name.textAlignment = NSTextAlignment.center
        
        let nameStr = JPUserInfoViewModel.sharedUserInfoViewModel.userInfo?.name
        name.text = "欢迎回来,\(nameStr!)"
        name.alpha = 0
        
        return name
    }()
    
    override func loadView() {
        
        let welcomeImageView = UIImageView(image: UIImage(named: "ad_background"))
        view = welcomeImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }

    func setupUI() {
        
        view.addSubview(iconImageView)
        view.addSubview(nameLabel)
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImageView.snp_bottom).offset(8)
            make.width.equalTo(200)
            make.centerX.equalTo(iconImageView)
        }
    }
    
    func startAnimation() {
        
        iconImageView.snp_updateConstraints { (make) in
            make.centerY.equalTo(view).offset(-200)
        }
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            
                self.view.layoutIfNeeded()
            
            }) { (_) in
                
            UIView.animate(withDuration: 0.5, animations: { 
                self.nameLabel.alpha = 1
                }, completion: { (_) in
                    NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotification), object: nil)
            })
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
